$(function() {
    var duizhangstate;
    var page = 1;
    var tddata = [{
        key: 'order_no',
        type: 2,
        url: '{:url("order/detail")}',
        url: ''
    }, {
        key: 'ride_time',
        type: 1,

        id: 137
    }, {
        key: 'from_addr',
        type: 1,
        url: ''
    }, {
        key: 'to_addr',
        type: 1,
        url: ''
    }, {
        key: 'passenger_name',
        type: 1,
        url: ''
    }, {
        key: 'approval',
        type: 1,
        url: ''
    }, {
        key: 'order_type2',
        type: 1,
        url: ''
    }, {
        key: 'use_explain',
        type: 1,
        url: ''
    }, {
        key: 'booking_type2',
        type: 1,
        url: ''
    }, {
        key: 'driver_name',
        type: 1,
        url: ''
    }, {
        key: 'car_number',
        type: 1,
        url: ''
    }, {
        key: 'order_money',
        type: 1,
        url: ''
    }, {
        key: 'order_state2',
        type: 1,
        url: ''
    }, {
        key: 'state2',
        type: 1,
        url: ''
    }];
    var addoperation = ``

    $('.print').on('click', function() {
        window.print()
    })

    var arr;
    accdetail()

    function accdetail() {
        var detailload = layer.load()
        // var passenger_name = '';
        // var orderer = '';
        // var approval = '';
        $.ajax({
            type: 'post',
            url: "{:url('/adminapi/Check/detail')}",
            data: {
                id: '{$Think.get.id}',
                passenger_name: '{$Think.get.passenger_name}',
                orderer: '{$Think.get.orderer}',
                approval: '{$Think.get.approval}',
                order_type: '{$Think.get.order_type}',
                page: page
            },
            success: function(res) {
                console.log(res)
                if(res.code == 1) {
                    $('.company').html(res.data.car_use_name)
                    $('.month').html(res.data.month)
                    duizhangstate = res.data.check_state
                    if(duizhangstate == 1) {
                        $(".btn-check").html("审核");
                        //				$(".btn-check").attr('disabled','1');
                        $(".btn-check").removeClass("layui-btn-disabled");
                        $('.btn-check').addClass('am-btn-primary tpl-btn-bg-color-success');
                    } else if(duizhangstate == 2) {
                        $(".btn-check").html("重新审核");
                        $(".btn-check").removeClass("layui-btn-disabled ");
                        $('.btn-check').addClass('am-btn-primary tpl-btn-bg-color-success');
                        //				$('.btn-check').attr("disabled","disabled");
                    } else if(duizhangstate == 3) {
                        $(".btn-check").html("待确认");
                        $(".btn-check").removeClass("am-btn-primary tpl-btn-bg-color-success");
                        $('.btn-check').addClass('layui-btn-disabled');
                        $('.btn-check').attr("disabled", "disabled");
                    } else if(duizhangstate == 4) {
                        $(".btn-check").html("已确定");
                        $(".btn-check").removeClass("layui-btn-disabled");
                        $('.btn-check').addClass('am-btn-primary tpl-btn-bg-color-success');
                        $('.btn-check').attr("disabled", "disabled");
                    }
                    var orderlist = res.data.items
                    for(var idx = 0; idx < orderlist.length; idx++) {
                        if(orderlist[idx].state == 1) {
                            orderlist[idx].state2 = '有效'
                        } else {
                            orderlist[idx].state2 = '无效'
                        }
                        if(orderlist[idx].order_type == 1) {
                            orderlist[idx].order_type2 = '公务订单'
                            //						订单类型,1：，2：企业订单，3：个人订单
                        } else if(orderlist[idx].order_type == 2) {
                            orderlist[idx].order_type2 = '企业订单'
                        } else if(orderlist[idx].order_type == 3) {
                            orderlist[idx].order_type2 = '个人订单'
                        }

                        if(orderlist[idx].booking_type == 1) {
                            //				订车类型：1，。2，。3，
                            orderlist[idx].booking_type2 = '订车（日常）'
                        } else if(orderlist[idx].booking_type == 2) {
                            orderlist[idx].booking_type2 = '包车'
                        } else if(orderlist[idx].booking_type == 3) {
                            orderlist[idx].booking_type2 = '分时租赁'
                        }

                        if(orderlist[idx].order_state == 1) {
                            orderlist[idx].order_state2 = '待审批'
                        } else if(orderlist[idx].order_state == 2) {
                            orderlist[idx].order_state2 = '已批准'
                        } else if(orderlist[idx].order_state == 3) {
                            orderlist[idx].order_state2 = '已拒绝'
                        } else if(orderlist[idx].order_state == 4) {
                            orderlist[idx].order_state2 = '待用车'
                        } else if(orderlist[idx].order_state == 5) {
                            orderlist[idx].order_state2 = '已派车'
                        } else if(orderlist[idx].order_state == 6) {
                            orderlist[idx].order_state2 = '接驾中'
                        } else if(orderlist[idx].order_state == 7) {
                            orderlist[idx].order_state2 = '已到达出发地'
                        } else if(orderlist[idx].order_state == 8) {
                            orderlist[idx].order_state2 = '已到达目的地'
                        } else if(orderlist[idx].order_state == 9) {
                            orderlist[idx].order_state2 = '待支付'
                        } else if(orderlist[idx].order_state == 10) {
                            orderlist[idx].order_state2 = '待评价'
                        } else if(orderlist[idx].order_state == 11) {
                            orderlist[idx].order_state2 = '已完成'
                        } else if(orderlist[idx].order_state == 12) {
                            orderlist[idx].order_state2 = '已取消'
                        } else if(orderlist[idx].order_state == 13) {
                            orderlist[idx].order_state2 = '已终止'
                        } else if(orderlist[idx].order_state == 14) {
                            orderlist[idx].order_state2 = '无效'
                        }
                    }
                    $('tbody').html(tbodymainf2(orderlist, 1, tddata, addoperation, 'id'))

                    $('.pagination').html(pagechang1(res.data.count, 10, page))
                    $('.laquo').on('click', function() {
                        page -= 1;
                        console.log(page)
                        accdetail()
                    })
                    $('.pageli').on('click', function() {
                        page = $(this).html()
                        console.log(page)
                        accdetail()
                    })
                    //下一页
                    $('.raquo').on('click', function() {
                        page += 1;
                        accdetail()
                    });
                    for(var ii = 0; ii < orderlist.length; ii++) {
                        if(orderlist[ii].state == 1) {
                            $('tbody').find('tr').eq(ii).append(`
						<td>
						<div class="tpl-table-black-operation">
							<a class="invalid">
								<i class="am-icon-pencil"></i> 无效
							</a>
							</div>
						</td>		
					`)
                        } else {
                            $('tbody').find('tr').eq(ii).append(`<td>订单无效</td>`)
                        }
                    }
                    layer.close(detailload)
                    if(res.code == 1) {}
                    //失效
                    $('.invalid').on('click', function() {
                        var that = this;
                        var invalididx = $('.invalid').closest('tr').data('id')
                        layer.open({
                            content: '该订单是否失效',
                            btn: ['确定', '取消'],
                            yes: function() {
                                var invalidload = layer.load()
                                $.ajax({
                                    type: "post",
                                    url: "{:url('adminapi/Check/cancel')}",
                                    data: {
                                        id: invalididx
                                    },
                                    success: function(res2) {
                                        console.log(res2)
                                        if(res2.code == 1) {
                                            layer.close()
                                            Msg('失效成功')
                                            accdetail()
                                        } else {
                                            Err('失效失败')
                                        }
                                    }
                                });
                            }
                        });

                    })
                }else{
                    Err(res.msg)
                    layer.close(detailload)
                }
            },
            error:function(){
                layer.close(detailload)
                Err('请求失败')
            }
        })
    }
    $('.btn-check').on('click', function() {
        var checkload = layer.load()
        $.ajax({
                type: "post",
                url: "{:url('adminapi/Check/audit')}",
                data: {
                    id: {$Think.get.id}
            },
            success: function(res) {
            layer.close(checkload)
            console.log(res)
            if(res.code == 1) {
                Msg('对账单审核已发送')
                accdetail()
            } else {
                Msg(res.msg)
            }
        }
    });
    })
    $('.btn-back').on('click', function() {
        window.history.back(-1);
    })
})